package com.wgts.wgts.mvp.view.activity.homeclasschildactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.labels.LabelData;
import com.wgts.wgts.control.labels.LabelsView;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerShopClassChildActivityComponent;
import com.wgts.wgts.dagger.module.ShopClassChildActivityModule;
import com.wgts.wgts.mvp.presenter.IShopClassChildActivityPresenter;
import com.wgts.wgts.mvp.view.IShopClassChildActivityView;
import com.wgts.wgts.mvp.view.activity.classification.ClassificationActivity;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.netmodle.resp.SearchShopResultEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/1914:35
 *
 *   首页商品分类点击的子页面
 *
 */public class ShopClassChildActivity extends BaseActivity implements IShopClassChildActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.screen_normal_text)
    TextView screenNormalText;
    @BindView(R.id.screen_normal_ly)
    LinearLayout screenNormalLy;
    @BindView(R.id.screen_sell_text)
    TextView screenSellText;
    @BindView(R.id.screen_sell_icon)
    ImageView screenSellIcon;
    @BindView(R.id.screen_sell_ly)
    LinearLayout screenSellLy;
    @BindView(R.id.screen_price_text)
    TextView screenPriceText;
    @BindView(R.id.screen_price_icon)
    ImageView screenPriceIcon;
    @BindView(R.id.screen_price_ly)
    LinearLayout screenPriceLy;
    @BindView(R.id.screen_button_ly)
    LinearLayout screenButtonLy;
    @BindView(R.id.shop_class_child_recycle)
    RecyclerView shopClassChildRecycle;
    @BindView(R.id.dismiss_screen_ly)
    LinearLayout dismissScreenLy;
    @BindView(R.id.screen_popup_title_one)
    TextView screenPopupTitleOne;
    @BindView(R.id.screen_lab_one)
    LabelsView screenLabOne;
    @BindView(R.id.screen_popup_title_two)
    TextView screenPopupTitleTwo;
    @BindView(R.id.screen_low_price_edit)
    EditText screenLowPriceEdit;
    @BindView(R.id.screen_hight_price_edit)
    EditText screenHightPriceEdit;
    @BindView(R.id.screen_popup_title_three)
    TextView screenPopupTitleThree;
    @BindView(R.id.screen_lab_two)
    LabelsView screenLabTwo;
    @BindView(R.id.screen_popup_title_four)
    TextView screenPopupTitleFour;
    @BindView(R.id.screen_lab_three)
    LabelsView screenLabThree;
    @BindView(R.id.screen_reset)
    LinearLayout screenReset;
    @BindView(R.id.screen_confirm)
    LinearLayout screenConfirm;
    @BindView(R.id.screen_ly)
    LinearLayout screenLy;
    @BindView(R.id.show_screen_ly)
    FrameLayout showScreenLy;
    @BindView(R.id.no_touch_page)
    LinearLayout noTouchPage;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.brand_ly)
    LinearLayout brandLy;
    @BindView(R.id.type_ly)
    LinearLayout typeLy;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private Animation showAnim, hiddenAnim, dimssShow, dimssHidden;

    private String titleText;

    private List<SearchShopResultEntity.ShopList> shopList;

    //是否展示筛选框
    private boolean showScreen = true;

    private ShopClassChildRecycleAdapter adapter;

    //导航栏选中状态
    private boolean normalSelect = false, sellSelect = false, priceSelect = false;

    //SearchShopListRequest数据
    private int typeId;

    private Integer brandId;

    private Integer screenId;

    private Double minPrice, maxPrice;

    private String name, order, sort;

    public static final String ID = "id";

    public static final String NAME = "name";

    public static final String CLASS_THREE = "CLASS_THREE";

    public static final String INTO_TYPE = "into_type";

    public Integer secondTypeId;

    public static final String SECONDTYPEID = "secondTypeId";

    //判断是否第一次没进行类别选中进行筛选
    private boolean isFirst = false;

    @Inject
    IShopClassChildActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop_class_child_activity);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        typeId = Integer.parseInt(intent.getStringExtra(ID));
        screenId = typeId;
        includeTitle.setText(intent.getStringExtra(NAME));
        includeText.setVisibility(View.GONE);
        includeRightImage.setVisibility(View.VISIBLE);
        initView();

    }

    @Override
    public void init() {
        DaggerShopClassChildActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .shopClassChildActivityModule(new ShopClassChildActivityModule(this))
                .build()
                .inject(this);
        shopList = new ArrayList<>();
    }

    private void initView() {
        refreshLayout.setEnableRefresh(true);
        shopClassChildRecycle.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ShopClassChildRecycleAdapter(this, shopList);
        shopClassChildRecycle.setAdapter(adapter);
        adapter.setOnItemClickListener(new ShopClassChildRecycleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(ShopClassChildActivity.this, JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                intent.putExtra("id", shopList.get(position).productId);
                startActivity(intent);
            }
        });

        if (getIntent().getStringExtra(INTO_TYPE) != null && getIntent().getStringExtra(INTO_TYPE).equals(CLASS_THREE)) {
            secondTypeId = getIntent().getIntExtra(SECONDTYPEID, 0);
            presenter.getScreen(secondTypeId);
        } else {
            presenter.getScreen(typeId);
        }
        showDialog();
        presenter.getShopList(typeId, name, brandId, minPrice, maxPrice, order, sort);
        refresh();
        loadMore();
    }

    @OnClick({R.id.back_btn_ly, R.id.include_right_ly, R.id.screen_normal_ly, R.id.screen_sell_ly,
            R.id.screen_price_ly, R.id.screen_button_ly, R.id.dismiss_screen_ly, R.id.screen_reset,
            R.id.screen_confirm, R.id.net_error_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.include_right_ly:
                search();
                break;
            case R.id.screen_normal_ly:
                screenNavigation(1, false);
                break;
            case R.id.screen_sell_ly:
                screenNavigation(2, sellSelect);
                break;
            case R.id.screen_price_ly:
                screenNavigation(3, priceSelect);
                break;
            case R.id.screen_button_ly:
                screen(showScreen);
                break;
            case R.id.dismiss_screen_ly:
                screen(false);
                break;
            case R.id.screen_reset:
                reset();
                break;
            case R.id.screen_confirm:
                confirm();
                break;
            case R.id.net_error_ly:
                presenter.getShopList(screenId, name, brandId, minPrice, maxPrice, order, sort);
                break;
        }
    }


    //确认筛选方法
    private void confirm() {
        order = "";
        sort = "";
        if (screenLowPriceEdit.getText().toString().trim() != null && !screenLowPriceEdit.getText()
                .toString().trim().equals("")) {
            minPrice = Double.valueOf(screenLowPriceEdit.getText().toString().trim());
        }else {
            minPrice = null;
        }
        if (!isFirst){
            titleText = "全部";
        }
        if (getIntent().getStringExtra(INTO_TYPE) != null && getIntent().getStringExtra(INTO_TYPE).equals(CLASS_THREE)){
            if (!isFirst){
                screenId = getIntent().getIntExtra(SECONDTYPEID, 0);
            }
        }
        if (screenHightPriceEdit.getText().toString().trim() != null && !screenHightPriceEdit.getText()
                .toString().trim().equals("")) {
            maxPrice = Double.valueOf(screenHightPriceEdit.getText().toString().trim());
        }else {
            maxPrice = null;
        }
        includeTitle.setText(titleText);
        presenter.getShopList(screenId, name, brandId, minPrice, maxPrice, order, sort);
        screenNormalText.setSelected(true);
        screenSellText.setSelected(false);
        screenSellIcon.setImageResource(R.mipmap.ic_sift_open);
        screenPriceText.setSelected(false);
        screenPriceIcon.setImageResource(R.mipmap.ic_sift_open);
        sellSelect = false;
        priceSelect = false;
        noTouchPage.setVisibility(View.VISIBLE);
        showScreenLy.startAnimation(hiddenAnim);
        dismissScreenLy.startAnimation(dimssHidden);
        hiddenAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                noTouchPage.setVisibility(View.GONE);
                screenLy.setVisibility(View.GONE);
                showScreen = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    //筛选重置方法
    private void reset() {
        screenLabOne.setSelects(0);
        brandId = null;
        maxPrice = null;
        minPrice = null;
        screenLowPriceEdit.setText("");
        screenHightPriceEdit.setText("");
        screenLabOne.clearAllSelect();
        screenLabTwo.clearAllSelect();
        screenLabThree.clearAllSelect();
    }

    //筛选导航栏点击事件
    private void screenNavigation(int position, boolean isSelect) {
        switch (position) {
            case 1:
                screenNormalText.setSelected(true);
                screenSellText.setSelected(false);
                screenSellIcon.setImageResource(R.mipmap.ic_sift_open);
                screenPriceText.setSelected(false);
                screenPriceIcon.setImageResource(R.mipmap.ic_sift_open);
                sellSelect = false;
                priceSelect = false;
                order = "";
                sort = "";
                presenter.getShopList(screenId, name,
                        brandId, minPrice, maxPrice, order, sort);
                break;
            case 2:
                if (isSelect) {
                    screenNormalText.setSelected(false);
                    screenSellText.setSelected(true);
                    screenSellIcon.setImageResource(R.mipmap.ic_sift_main_pack);
                    screenPriceText.setSelected(false);
                    screenPriceIcon.setImageResource(R.mipmap.ic_sift_open);
                    sellSelect = false;
                    priceSelect = false;
                    order = "SELL";
                    sort = "ASC";
                    presenter.getShopList(screenId, name,
                            brandId, minPrice, maxPrice, order, sort);
                } else {
                    screenNormalText.setSelected(false);
                    screenSellText.setSelected(true);
                    screenSellIcon.setImageResource(R.mipmap.ic_sift_pack);
                    screenPriceText.setSelected(false);
                    screenPriceIcon.setImageResource(R.mipmap.ic_sift_open);
                    sellSelect = true;
                    priceSelect = false;
                    order = "SELL";
                    sort = "DESC";
                    presenter.getShopList(screenId, name,
                            brandId, minPrice, maxPrice, order, sort);
                }
                break;
            case 3:
                if (isSelect) {
                    screenNormalText.setSelected(false);
                    screenSellText.setSelected(false);
                    screenSellIcon.setImageResource(R.mipmap.ic_sift_open);
                    screenPriceText.setSelected(true);
                    screenPriceIcon.setImageResource(R.mipmap.ic_sift_main_pack);
                    sellSelect = false;
                    priceSelect = false;
                    order = "PRICE";
                    sort = "ASC";
                    presenter.getShopList(screenId, name,
                            brandId, minPrice, maxPrice, order, sort);
                } else {
                    screenNormalText.setSelected(false);
                    screenSellText.setSelected(false);
                    screenSellIcon.setImageResource(R.mipmap.ic_sift_open);
                    screenPriceText.setSelected(true);
                    screenPriceIcon.setImageResource(R.mipmap.ic_sift_pack);
                    sellSelect = false;
                    priceSelect = true;
                    order = "PRICE";
                    sort = "DESC";
                    presenter.getShopList(screenId, name,
                            brandId, minPrice, maxPrice, order, sort);
                }
                break;
        }
    }

    //筛选栏目展示
    private void screen(boolean isShow) {
        showAnim = AnimationUtils.loadAnimation(this, R.anim.screen_show);
        hiddenAnim = AnimationUtils.loadAnimation(this, R.anim.screen_hidden);
        dimssShow = AnimationUtils.loadAnimation(this, R.anim.dimss_screen_show);
        dimssHidden = AnimationUtils.loadAnimation(this, R.anim.dimss_screen_hidden);
        if (isShow) {
            screenLy.setVisibility(View.VISIBLE);
            showScreenLy.startAnimation(showAnim);
            dismissScreenLy.startAnimation(dimssShow);
            showScreen = false;
        } else {
            noTouchPage.setVisibility(View.VISIBLE);
            showScreenLy.startAnimation(hiddenAnim);
            dismissScreenLy.startAnimation(dimssHidden);
            hiddenAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    noTouchPage.setVisibility(View.GONE);
                    screenLy.setVisibility(View.GONE);
                    showScreen = true;
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
    }

    //搜索方法
    private void search() {
        Intent intent = new Intent(ShopClassChildActivity.this, ClassificationActivity.class);
        startActivity(intent);
    }

    //lab选中事件
    private void labClick() {
        //标签选中事件，有样式改变
        screenLabOne.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
            @Override
            public void onLabelSelectChange(View label, LabelData data, boolean isSelect, int position) {
                //TextView textView = (TextView) label;
                if (isSelect) {
                    isFirst = true;
                    ((TextView) label).setTextColor(ContextCompat.getColor(ShopClassChildActivity.this, R.color.whrite));
                    screenId = data.getId();
                    titleText = data.getName();
                } else {
                    ((TextView) label).setTextColor(ContextCompat.getColor(ShopClassChildActivity.this, R.color.black));
                }
            }
        });

        screenLabTwo.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
            @Override
            public void onLabelSelectChange(View label, LabelData data, boolean isSelect, int position) {
                //TextView textView = (TextView) label;
                if (isSelect) {
                    ((TextView) label).setTextColor(ContextCompat.getColor(ShopClassChildActivity.this, R.color.whrite));
                    brandId = data.getId();
                } else {
                    ((TextView) label).setTextColor(ContextCompat.getColor(ShopClassChildActivity.this, R.color.black));
                    brandId = null;
                }
            }
        });

    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        netErrorLy.setVisibility(View.VISIBLE);
        noneDataLy.setVisibility(View.GONE);
    }

    @Override
    public void getShopList(SearchShopResultEntity entity, boolean noMore) {
        dialogDismiss();
        refreshLayout.finishRefresh();
        adapter.setNoMore(noMore);
        if (entity.list.size() > 0){
            noneDataLy.setVisibility(View.GONE);
            netErrorLy.setVisibility(View.GONE);
            shopList.clear();
            shopList.addAll(entity.list);
            adapter.notifyDataSetChanged();
        }else {
            noneDataLy.setVisibility(View.VISIBLE);
            netErrorLy.setVisibility(View.GONE);
        }
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
    }

    @Override
    public void screenDate(List<LabelData> typeScreenList, List<LabelData> brandScreenList) {
        if (typeScreenList.size() > 0) {
            typeLy.setVisibility(View.VISIBLE);
        } else {
            typeLy.setVisibility(View.GONE);
        }
        if (brandScreenList.size() > 0) {
            brandLy.setVisibility(View.VISIBLE);
        } else {
            brandLy.setVisibility(View.GONE);
        }
        screenLabOne.setLabels(typeScreenList);
        screenLabTwo.setLabels(brandScreenList);
        labClick();
    }

    @Override
    public void loadMore(SearchShopResultEntity entity, boolean noMore) {
        refreshLayout.finishLoadmore();
        adapter.setNoMore(noMore);
        shopList.addAll(entity.list);
        adapter.notifyDataSetChanged();
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
    }

    private void refresh() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.getShopList(screenId, name, brandId, minPrice, maxPrice, order, sort);
            }
        });
    }

    private void loadMore() {
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMore(screenId, name, brandId, minPrice, maxPrice, order, sort);
            }
        });
    }
}
